package com.his.his_system.controller.VisitController.ZFB;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.his.his_system.controller.VisitController.Query;
import com.his.his_system.domain.DzmHisCarePaylog;
import com.his.his_system.domain.DzmHisCarePkg;
import com.his.his_system.domain.DzmHisCareRefundlog;
import com.his.his_system.domain.DzmHisRegistration;
import com.his.his_system.service.DzmHisCarePaylogService;
import com.his.his_system.service.DzmHisCarePkgService;
import com.his.his_system.service.DzmHisCareRefundlogService;
import com.his.his_system.service.DzmHisRegistrationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;

@RestController
@RequestMapping("/app/")
@Slf4j
public class RegisterController {

    @Resource
    private  DzmHisCareRefundlogService dzmHisCareRefundlogService;
    @Resource
    private DzmHisRegistrationService dzmHisRegistrationService;

    @Resource
    private DzmHisCarePkgService dzmHisCarePkgService;

    @Resource
    private DzmHisCarePaylogService dzmHisCarePaylogService;
    /**
     * 支付或退款
     * @param
     * @param
     * @return
     */
    @GetMapping("payOrRef")
    @Transactional
    public void payOrRef(HttpServletResponse resp){
        Integer registrationId = AliPayController.registrationid;
        Integer i=AliPayController.ii;

        QueryWrapper<DzmHisRegistration> queryWrapper =new QueryWrapper();
        queryWrapper.eq("registration_id",registrationId);
        DzmHisRegistration dzmHisRegistration =   dzmHisRegistrationService.getOne(queryWrapper);
        //退款
        if(i==1){
            //1挂号表状态修改
            UpdateWrapper<DzmHisRegistration> updateWrapper1=new UpdateWrapper();
            updateWrapper1
                    .eq("registration_id",registrationId)
                    .set("registration_status",3);
            boolean a=  dzmHisRegistrationService.update(updateWrapper1);
            //2收费表状态修改
            UpdateWrapper<DzmHisCarePkg> updateWrapper2=new UpdateWrapper();
            updateWrapper2
                    .eq("registration_id",registrationId)
                    .set("status",4);
            boolean b=  dzmHisCarePkgService.update(updateWrapper2);
            if(a&&b){
                try {
                    //退款成功
                    DzmHisCareRefundlog dzmHisCareRefundlog = new DzmHisCareRefundlog();
                    dzmHisCareRefundlog.setPaymentPlatform(2);
                    dzmHisCareRefundlog.setRefundAmount(BigDecimal.valueOf(dzmHisRegistration.getRegistrationAmount()) );
                    dzmHisCareRefundlog.setStatus(1);
                    dzmHisCareRefundlog.setAddtime(new Date());
                    if(!dzmHisCareRefundlogService.save(dzmHisCareRefundlog)){
                        log.error("添加退款记录失败");
                        resp.sendRedirect("http://localhost:8080/#/registerlist");
                    }
                    resp.sendRedirect("http://localhost:8080/#/registerlist");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else {
                try {
                resp.sendRedirect("http://localhost:8080/#/registerlist");
            } catch (IOException e) {
                e.printStackTrace();
            }

            }

        }else {
            //付款
            //1挂号表状态修改
            UpdateWrapper<DzmHisRegistration> updateWrapper1=new UpdateWrapper();
            updateWrapper1
                    .eq("registration_id",registrationId)
                    .set("registration_status",1);
            boolean a=  dzmHisRegistrationService.update(updateWrapper1);
            //2收费表状态修改
            UpdateWrapper<DzmHisCarePkg> updateWrapper2=new UpdateWrapper();
            updateWrapper2
                    .eq("registration_id",registrationId)
                    .set("status",1);
            boolean b=  dzmHisCarePkgService.update(updateWrapper2);
            if(a&&b){
                try {
                    //付款成功
                    addfukuan(resp,BigDecimal.valueOf(dzmHisRegistration.getRegistrationAmount()));
                    resp.sendRedirect("http://localhost:8080/#/registerlist");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else {
                try {
                    resp.sendRedirect("http://localhost:8080/#/registerlist");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public  void  addfukuan(HttpServletResponse resp,BigDecimal bg) throws IOException {
        DzmHisCarePaylog dzmHisCarePaylog =new DzmHisCarePaylog();
        dzmHisCarePaylog.setPaymentPlatform(2);
        dzmHisCarePaylog.setPayAmount(bg);
        dzmHisCarePaylog.setStatus(1);
        dzmHisCarePaylog.setAddtime((int)(System.currentTimeMillis()/1000));
        if(!dzmHisCarePaylogService.save(dzmHisCarePaylog)){
            log.error("添加付款记录失败");
            resp.sendRedirect("http://localhost:8080/#/registerlist");
        }
    }
}
